package Persistencia;

import java.sql.*;
import java.util.ArrayList;

import DAO.DAOAutomovil;
import Modelo.Automovil;
import Modelo.Cliente;

public class AdministracionPersistenciaAutomovil{
	
	private static AdministracionPersistenciaAutomovil instance;
	
	private AdministracionPersistenciaAutomovil(){
		
	}
	 
	public static AdministracionPersistenciaAutomovil getInstance(){
		if(instance==null)
			instance = new AdministracionPersistenciaAutomovil();
		return instance;
	}

	public void insert(DAOAutomovil automovil){
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try{
			PreparedStatement ps = con.prepareStatement("insert into Automoviles values (?,?,?,?,?,?,?)");
			ps.setString(1, automovil.getPatente());
			ps.setInt(2, automovil.getKilometraje());
			ps.setString(3, automovil.getCombustibleTipo());
			ps.setString (4, automovil.getEstado());
			ps.setInt(5, automovil.getAnio());
			ps.setString (6,automovil.getModeloMarca());
			ps.setString(7, automovil.getModeloDescripcion());
			ps.execute();
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.out.println("Error al insertar Automovil "+e.getMessage());
		}
	}

	public void update(DAOAutomovil automovil){
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try{
			PreparedStatement ps = con.prepareStatement("" +
					"update Automoviles set apellido = ?, kilometraje = ?, combustibleTipo = ?, estado =? ,anio = ?, modeloMarca = ? , modeloDescripcion =?" +
					"where patente = ?");
			ps.setInt(1, automovil.getKilometraje());
			ps.setString(2, automovil.getCombustibleTipo());
			ps.setString (3, automovil.getEstado());
			ps.setInt(4, automovil.getAnio());
			ps.setString (5,automovil.getModeloMarca());
			ps.setString(6, automovil.getModeloDescripcion());
			ps.setString(7, automovil.getPatente());
			ps.execute();
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.out.println("Error al modificar Automovil"+e.getMessage());
		}
	}

	public void delete(DAOAutomovil p){
		Connection con = PoolConnection.getPoolConnection().getConnection();
		try{
						
			PreparedStatement ps2 = con.prepareStatement("delete from Automoviles where patente = ?");
			ps2.setString(1, p.getPatente());
			ps2.execute();
			
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.out.println("Error al borrar Automovil "+e.getMessage());
		}
	}
	
	public ArrayList<Automovil> select(){
		ArrayList<Automovil> result = new ArrayList<Automovil>();
		Connection con = PoolConnection.getPoolConnection().getConnection();
	
		try{
			String query = "SELECT * FROM Automoviles "; 
			
			PreparedStatement ps = con.prepareStatement(query);
			//ps.setString(1,descripcion);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Automovil rec = new Automovil(rs.getString(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getString(6));	
				result.add(rec);				
			}
			
		
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.err.print("Error al Buscar Clientes");
			PoolConnection.getPoolConnection().closeConnections();
		}
		
		return result;
	}
	
	public Automovil select(String patente){
		ArrayList<Automovil> result = new ArrayList<Automovil>();
		Connection con = PoolConnection.getPoolConnection().getConnection();
	
		try{
			String query = "SELECT * FROM Automoviles where patente = ?"; 
			
			PreparedStatement ps = con.prepareStatement(query);
			ps.setString(1,patente);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Automovil rec = new Automovil(rs.getString(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getString(6));	
				result.add(rec);				
			}
			
		
			PoolConnection.getPoolConnection().closeConnections();
		}catch(Exception e){
			System.err.print("Error al Buscar Clientes");
			PoolConnection.getPoolConnection().closeConnections();
		}
		
		return result.get(0);
	}

}
	